﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_TrainingCourseware : DLL.MyAdminPage
{
	private DLL.Courseware.Component clsCoursewareComp;
	private DLL.TrainingCourseware.Component clsTrainingCourewareComp;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			clsControl.ShowMySelfCanAddCoursewareTraining(ddlTraining, nUserID);
			clsControl.ShowCoursewareSort(ddlCoursewareSort);

			if (ddlTraining.Items.Count == 0)
			{
				idTotal.Visible = false;
			}

			BindGridView();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		clsCoursewareComp = new DLL.Courseware.Component(Page);
		clsTrainingCourewareComp = new DLL.TrainingCourseware.Component(Page);

		base.OnInit(e);

		BtnAdd.Click += new EventHandler(BtnAdd_Click);
		ddlTraining.SelectedIndexChanged += new EventHandler(ddlTraining_SelectedIndexChanged);
		Grv.RowDataBound += new GridViewRowEventHandler(Grv_RowDataBound);
		Grv.RowCommand += new GridViewCommandEventHandler(Grv_RowCommand);
	}

	void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		if (e.CommandName == "MyDelete")
		{
			DLL.TrainingCourseware.Entity clsDelete = new DLL.TrainingCourseware.Entity();

			clsDelete = clsTrainingCourewareComp.GetOne(Convert.ToInt32(e.CommandArgument));

			clsTrainingCourewareComp.Delete(clsDelete);

			BindGridView();
		}
	}

	void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		int nID = 0;

		if (e.Row.RowIndex >= 0)
		{
			nID = Convert.ToInt32(((GridView) sender).DataKeys[e.Row.RowIndex].Value);

			HyperLink hLink = new HyperLink();

			hLink = (HyperLink) e.Row.FindControl("hLinkPlayOnLine");
			hLink.Target = "_blank";
			hLink.NavigateUrl = "~/CuPlay/OnLinePlay.aspx?Path=" +clsTrainingCourewareComp.GetOne(nID).CoursewarePath;

			LinkButton lBtnDelete = new LinkButton();

			lBtnDelete = (LinkButton)e.Row.FindControl("LBtnDelete");
			lBtnDelete.CommandName = "MyDelete";
			lBtnDelete.CommandArgument = nID.ToString();
		}
	}

	void ddlTraining_SelectedIndexChanged(object sender, EventArgs e)
	{
		BindGridView();
	}

	void BtnAdd_Click(object sender, EventArgs e)
	{
		DLL.Courseware.Entity clsCoureware = new DLL.Courseware.Entity();

		clsCoureware.UserID = nUserID;
		clsCoureware.DepartmentID = nDepartmentID;
		clsCoureware.SortID = Convert.ToInt32(ddlCoursewareSort.Text);
		clsCoureware.Public = false;

		if (clsCoursewareComp.Add(AttachFile, clsCoureware))
		{
			DLL.TrainingCourseware.Entity clsTrainingCourseware = new DLL.TrainingCourseware.Entity();

			clsTrainingCourseware.TrainingID = Convert.ToInt32(ddlTraining.Text);
			clsTrainingCourseware.CoursewareID = clsCoursewareComp.GetNewest().ID;
			clsTrainingCourseware.Number = Convert.ToInt32(TbNumber.Text);

			if (clsTrainingCourewareComp.Add(clsTrainingCourseware))
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "课件上传成功！");
				BindGridView();
			}
		}
	}

	private void BindGridView()
	{
		try
		{
			Grv.DataSource = clsTrainingCourewareComp.QueryOneTrainingCourseware(Convert.ToInt32(ddlTraining.Text));
			Grv.DataKeyNames = new string[] { "ID" };
			Grv.DataBind();
		}
		catch
		{
			TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "没有可以添加课件的培训班！");
		}
	}
}